/**
 * Created by lupf on 2016/4/28.
 */

angular.module('myApp')

    .controller('queryCtrl',['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'myConstant', 'growl', 'dateFilter', 'DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                                                                CacheService, limitToFilter, filterFilter, myConstant, growl, dateFilter,DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            vm.validateOptions = {
                blurTrig: true,
                showError: function (elem, errorMessages) {
                    growl.addErrorMessage(errorMessages);
                    // angular.element(elem).after('<span>' + errorMessages + '</span>');
                },
                removeError: true
            };

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                TYPE_OPERATION: {},
                USER_STATUS: {},
                OPERATION_RESULT: {},
                MERCHANT_CODE: {}
            };


            CacheService.initCache(vm.cached);

            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.checkStartDay=checkStartDay;
            vm.checkEndDay=checkEndDay;
            /*------------------以上配置通用---------------------*/


            /*------------------以上方法名可选择性通用---------------------*/

            /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
             $log.debug(cacheKey, cacheObj);
             vm.cached[cacheKey] = cacheObj;
             });*/

            /*vm.getCache = function (key) {
             CacheService.getCache(key)
             }*/

            function queryDetail() {
                vm.url = config.ctx + '/system/operationQuery/search';

                //组织参数：查询条件 + 分页数据
                var postData = {};
                postData= angular.copy(vm.queryBean);
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                postData.beginTime = dateFilter(postData.beginTime, 'yyyy-MM-dd');
                postData.endTime = dateFilter(postData.endTime, 'yyyy-MM-dd');
                $http.post(vm.url, $httpParamSerializerJQLike(postData))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function checkStartDay(beginTime,endTime) {
                var startD = new Date(beginTime);
                var endD   = new Date(endTime);
                if(startD > endD){
                    // alert("日期选择错误！请重新选择!");
                    vm.queryBean.beginTime = null;
                    // vm.queryBean.endTime = DateCalculateService.getToday();
                    return false;
                }
            }

            function checkEndDay(beginTime,endTime) {
                var startD = new Date(beginTime);
                var endD   = new Date(endTime);
                if(endD < startD){
                    // alert("日期选择错误！请重新选择!");
                    vm.queryBean.endTime = null;
                    // vm.queryBean.beginTime = DateCalculateService.getToday();
                    return false;
                }
            }


            function resetForm() {
                vm.queryBean = {};
            }

        }]);
